package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.WarCaseStudy;
import com.battle.pojo.entity.WarCaseStudyDetails;
import com.battle.pojo.param.warCaseStudyDetails.*;
import com.battle.pojo.vo.warCaseStudyDetails.*;
import com.battle.pojo.dto.warCaseStudyDetails.*;
import com.battle.service.repository.in.WarCaseStudyDetailsRepository;
import com.battle.service.repository.in.WarCaseStudyRepository;
import com.battle.service.service.in.WarCaseStudyDetailsService;
import nirvana.core.context.WebContext;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：战例研讨记录服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class WarCaseStudyDetailsServiceImpl implements WarCaseStudyDetailsService {

    private final WarCaseStudyDetailsRepository warCaseStudyDetailsRepository;
    private final WarCaseStudyRepository studyRepository;

    public WarCaseStudyDetailsServiceImpl(WarCaseStudyDetailsRepository warCaseStudyDetailsRepository, WarCaseStudyRepository studyRepository) {
        this.warCaseStudyDetailsRepository = warCaseStudyDetailsRepository;
        this.studyRepository = studyRepository;
    }


    /**
     * 创建战例研讨记录
     *
     * @param param
     */
    @Override
    public void createWarCaseStudyDetails(CreateWarCaseStudyDetailsParam param) {
        WarCaseStudyDetails warCaseStudyDetails = new WarCaseStudyDetails();
        BeanUtils.copyProperties(param, warCaseStudyDetails);
        Long id = IdGenerator.SNOW.generate();
        warCaseStudyDetails.setId(id);
        warCaseStudyDetailsRepository.create(warCaseStudyDetails);
    }

    /**
     * 修改战例研讨记录
     *
     * @param param
     */
    @Override
    public void editWarCaseStudyDetails(EditWarCaseStudyDetailsParam param) {
        Long id = param.getId();
        WarCaseStudyDetails warCaseStudyDetails = warCaseStudyDetailsRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, warCaseStudyDetails);
        warCaseStudyDetailsRepository.edit(warCaseStudyDetails);
    }

    /**
     * 移除战例研讨记录
     *
     * @param id
     */
    @Override
    public void removeWarCaseStudyDetails(Long id) {
        WarCaseStudyDetails warCaseStudyDetails= warCaseStudyDetailsRepository.getByIdCheck(id);
        WarCaseStudy study = studyRepository.getByIdCheck(warCaseStudyDetails.getStudyId());
        if (WebContext.getCurrentUserId().equals(study.getInitiatorId())) throw new BizException("只有发起人才可以邀请");
        warCaseStudyDetailsRepository.removeById(id);
    }

    /**
     * 启动/禁用战例研讨记录
     *
     * @param id
     */
    @Override
    public void enabledWarCaseStudyDetails(Long id) {
        WarCaseStudyDetails warCaseStudyDetails = warCaseStudyDetailsRepository.getByIdCheck(id);
        warCaseStudyDetails.setIsDisable(!warCaseStudyDetails.getIsDisable());
        warCaseStudyDetailsRepository.edit(warCaseStudyDetails);
    }

    @Override
    public void upvoteNumber(Long id) {
        WarCaseStudyDetails warCaseStudyDetails = warCaseStudyDetailsRepository.getByIdCheck(id);
        if (warCaseStudyDetails.getUpvoteNumber()==null)warCaseStudyDetails.setUpvoteNumber(0);
        warCaseStudyDetails.setUpvoteNumber(warCaseStudyDetails.getUpvoteNumber()+1);
        warCaseStudyDetailsRepository.updateById(warCaseStudyDetails);
    }

    @Override
    public void topNumber(Long id) {
        WarCaseStudyDetails warCaseStudyDetails = warCaseStudyDetailsRepository.getByIdCheck(id);
        WarCaseStudy study = studyRepository.getByIdCheck(warCaseStudyDetails.getStudyId());
        if (WebContext.getCurrentUserId().equals(study.getInitiatorId())) throw new BizException("只有发起人才可以邀请");
        if (warCaseStudyDetails.getIsTop()){
            warCaseStudyDetails.setIsTop(false);
        }else{
            warCaseStudyDetails.setIsTop(true);
        }
        warCaseStudyDetailsRepository.updateById(warCaseStudyDetails);
    }

    /**
     * 获取战例研讨记录详情
     *
     * @param id
     * @return
     */
    @Override
    public GetWarCaseStudyDetailsVo getWarCaseStudyDetails(Long id) {
        WarCaseStudyDetails warCaseStudyDetails = warCaseStudyDetailsRepository.getByIdCheck(id);
        GetWarCaseStudyDetailsVo vo = new GetWarCaseStudyDetailsVo();
        BeanUtils.copyProperties(warCaseStudyDetails, vo);
        return vo;
    }

    /**
     * 获取战例研讨记录 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetWarCaseStudyDetailsListVo> getWarCaseStudyDetailsList(GetWarCaseStudyDetailsListParam param) {

        SelectWarCaseStudyDetailsDto dto = new SelectWarCaseStudyDetailsDto();
        BeanUtils.copyProperties(param, dto);
        Page<GetWarCaseStudyDetailsListVo> page = warCaseStudyDetailsRepository.selectWarCaseStudyDetails(dto);
        List<GetWarCaseStudyDetailsListVo> list = page.getRecords();
        return new PageResult<>(list, page);

    }

    /**
     * 获取战例研讨记录下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetWarCaseStudyDetailsDownListVo> getWarCaseStudyDetailsDownList(GetWarCaseStudyDetailsDownListParam param) {
        WarCaseStudyDetails po = new WarCaseStudyDetails();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<WarCaseStudyDetails> warCaseStudyDetailsList = warCaseStudyDetailsRepository.getListByEntity(po);

        List<GetWarCaseStudyDetailsDownListVo> list = new ArrayList<>();
        for (WarCaseStudyDetails warCaseStudyDetails : warCaseStudyDetailsList) {
            GetWarCaseStudyDetailsDownListVo vo = new GetWarCaseStudyDetailsDownListVo();
            BeanUtils.copyProperties(warCaseStudyDetails, vo);
            list.add(vo);
        }
        return list;
    }


}



